<template>
  <div class="header-content">
    <div class="header-left">
      <div class="header-logo" @click="$router.push('/')">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <div class="header-context">
        <div>加盟咨询</div>
        <div>抖音直播</div>
      </div>
      <div class="header-phone">{{ service_phone }}</div>
    </div>
    <div class="header-right">
      <div class="header-address">
        <img src="@/assets/dw .png" alt="" />
        <div>{{ city }}</div>
        <img src="@/assets/箭头 .png" alt="" />
      </div>
      <div>
        <el-popover placement="bottom" trigger="click" v-if="loginStatus == 0">
          <div class="popup-box">
            <div class="popup-header">请选择一种方式登录</div>
            <div class="popup-body">
              <div @click.stop="getCode(1)">
                <img src="@/assets/图层 27.png" alt="抖音登录">
                <div style="text-align: center;">抖音登录</div>
              </div>
              <div @click.stop="getCode(2)">
                <img src="@/assets/图层 28.png" alt="微信登录">
                <div style="text-align: center;">微信登录</div>
              </div>
            </div>
          </div>
          <el-button size="medium" slot="reference" v-if="loginStatus == 0" round>登录</el-button>
        </el-popover>
        <el-popover placement="bottom" trigger="click" v-if="loginStatus == 1">
          <div class="popup-box">
            <div class="user-info">
              <div>
                <el-avatar size="large" :src="userinfo.full_avatar"></el-avatar>
              </div>
              <div class="info-context">
                <div class="user-phone">{{ userinfo.phone }}</div>
                <div class="user-identity">
                  <div>ID({{ userinfo.id }})</div>
                  <img v-if="userinfo.is_teacher == 0" src="@/assets/学员.png" alt="" />
                  <img v-if="userinfo.is_teacher == 1" src="@/assets/讲师.png" alt="" />
                </div>
              </div>
            </div>
            <div class="menu-list">
              <div class="menu-item" v-for="(item, index) in menuList" :key="index" @click="gotoNext(item.url,item.title)">
                <div class="menu-title">
                  <img :src="item.img" alt="" />
                  <div>{{ item.title }}</div>
                </div>
                <img src="@/assets/矩形 16.png" alt="" />
              </div>
            </div>
          </div>
          <el-button slot="reference" style="padding: 0 !important;background-color: transparent; border: none;">
            <el-avatar size="large" :src="userinfo.full_avatar"></el-avatar>
          </el-button>
        </el-popover>
      </div>
    </div>
  </div>
</template>

<script>
import getProvince from "@/utils/location.js"
import { wxqrcode, dyqrcode, getLogin, getdyLogin } from '@/api/user.js'
import { getServicePhone } from '@/api/index.js'
import { setToken } from '@/utils/auth.js'
export default {
  data() {
    return {
      menuList: [
        {
          img: require("@/assets/zhxx.png"),
          title: "账户信息",
          url: "/AccountInfo",
        },
        {
          img: require("../assets/图层 16.png"),
          title: "我的订单",
          url: "/myOrder",
        },
        {
          img: require("../assets/kscj.png"),
          title: "考试成绩",
          url: "/ExamScores",
        },
        {
          img: require("../assets/wdsc.png"),
          title: "我的收藏",
          url: "/questionWrong",
        },
        {
          img: require("../assets/zhxx.png"),
          title: "关于我们",
          url: "/AboutUs",
        },
        {
          img: require("../assets/图层 19.png"),
          title: "注销账号",
          url: "",
        },
      ],
      loginType: 1,
      loginStatus: 0,
      service_phone: '',
      city: '',
      circleUrl:
        "https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png",
      qrcode: '',
      userinfo: {}
    };
  },
  created() {
    // 从地址中获取code，开始登录
    if (this.$route.query.code != undefined) {
      let code1 = this.$route.query.code
      // wx微信登录
      if (this.$route.query.scopes == undefined) {
        this.wxlogin(code1)
      }
      // dy抖音登录
      if (this.$route.query.scopes != undefined) {
        // console.log(55)
        this.dylogin(code1)
      }
    }

    // this.getCity()
    this.getServicePhone()
  },
  mounted() {
    // console.log(68,this.$route.query.code)
    let user = localStorage.getItem("userInfo")

    if(user != null){
      this.userinfo = JSON.parse(user)
      this.loginStatus = 1
    }
  },
  methods: {
    selectLogin(type) {
      this.loginType = type;
    },
    gotoNext(url,title) {
      if (title == '注销账号') {
        this.logout()
      } else {
        this.$router.push(url)
      }
    },
    bindPhone(){
      this.$prompt('检测到当前未绑定手机号，为了平台数据的统一性，请输入手机号', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^1[3456789]\d{9}$/,
          inputErrorMessage: '手机号格式不正确'
        }).then(({ value }) => {
          this.$message({
            type: 'success',
            message: '你的邮箱是: ' + value
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
    },
    // 注销账号
    logout(){
      this.$confirm('是否将该账号进行注销?', '提示', {
          confirmButtonText: '确认注销',
          cancelButtonText: '暂不注销',
          type: 'warning'
        }).then(() => {
          this.userinfo = {}
      this.loginStatus = 0
      localStorage.clear()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    },
    // 获取城市
    getCity() {
      let that = this
      getProvince().then(res => {
        that.city = res
      })
    },
    // 获取服务电话
    getServicePhone() {
      let that = this
      getServicePhone().then(res => {
        // console.log(9, res)
        that.service_phone = res.data.service_phone
      })
    },
    // 点击登录跳转
    getCode(type) {
      this.loginType = type;
      if (this.loginType == 1) {
        window.open(dyqrcode)
      }
      if (this.loginType == 2) {
        window.open(wxqrcode)
      }
    },
    // 根据code登录
    wxlogin(code) {
      let that = this
      getLogin({ code }).then(res => {
        that.loginStatus = 1
        // console.log(99, res)
        if (res.code == 200) {
          that.loginStatus = 1
          // 保存token
          setToken(res.data.access_token)
        }
      })
    },
    dylogin(code) {
      let that = this
      getdyLogin({ code }).then(res => {

        if (res.code == 200) {
          that.loginStatus = 1
          // 保存token
          setToken(res.data.access_token)
          that.userinfo = res.data.user
          localStorage.setItem('userInfo',JSON.stringify(res.data.user))
          // 绑定手机号
          if (res.data.user.phone) {
            // that.bindPhone()
          }
          // console.log(that.loginStatus)
        }
        // if()
      })
    }
  },
};
</script>

<style lang="scss">
.el-popover {
  padding: 0;
  border-radius: 10px;
  overflow: hidden;
}

.el-button {
  padding: 1vh 1.5vw !important;
  color: #2ac092;
}

.el-button:focus,
.el-button:hover,
.el-button:active {
  color: #fff;
  border-color: #2ac092;
  background-color: #2ac092;
}

.header-content {
  padding: 20px 12.5%;
  display: flex;
  justify-content: space-between;
  background-color: #2ac092;

  .header-left {
    display: flex;
    align-items: center;

    .header-logo {
      cursor: pointer;
      width: 30vw;
      height: 6.5vh;
      margin-right: 18px;

      >img {
        width: 100%;
        height: 100%;
      }
    }

    .header-context {
      color: #fff;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 100%;
      font-size: 1vw;
      margin-right: 1vw;

      >div {
        cursor: pointer;
      }
    }

    .header-phone {
      color: #fff;
      font-size: 1.5vw;
    }
  }

  .header-right {
    display: flex;
    align-items: center;

    .header-address {
      display: flex;
      color: #fff;
      margin-right: 0.5vw;
      font-size: 0.9vw;
      cursor: pointer;

      >img {
        width: 1.1vw;
        height: 2.3vh;
      }

      >div {
        margin: 0 0.25vw;
      }
    }
  }
}

.popup-header {
  height: 2vw;
  line-height: 2vw;
  // width: 12vw;
  font-size: 1vw;
  color: #999;
  text-align: center;
  background-color: #F5F5F6;
}

// .popup-tags {
//   display: flex;
//   color: #999999;
//   background: #f5f5f6;

//   >div {
//     padding: 1.2vh 0;
//     width: 50%;
//     text-align: center;
//     font-size: 1vw;
//     cursor: pointer;
//   }
// }

.popup-body {
  padding: 6vh 2vw 6vh 2vw;
  display: flex;
  // flex-direction: column;
  align-items: center;
  justify-content: space-around;

  >div {
    width: 4vw;
    height: 6vw;
    overflow: hidden;

    >img {
      display: inline-block;
      width: 100%;
      height: 4vw;
      margin: 0 auto;
    }
  }

  >div:nth-child(1) {
    margin-right: 1.5vw;
  }

  // .body-code {
  //   width: 12vw;
  //   height: 22vh;
  //   margin-bottom: 2vh;

  //   >img {
  //     width: 100%;
  //     height: 100%;
  //   }
  // }

  // .body-text {
  //   font-size: 0.8vw;

  //   >span {
  //     color: #2ac092;
  //   }
  // }
}

.isSelected {
  background: #fff;
  color: #2ac092;
  width: 100%;
  height: 100%;
}

.user-info {
  display: flex;
  background: #e6f5f0;
  align-items: center;
  padding: 25px 20px;

  .info-context {
    margin-left: 10px;
    font-size: 16px;
    color: #333;

    .user-identity {
      margin-top: 10px;
      display: flex;
    }
  }
}

.menu-list {
  .menu-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    font-size: 16px;
    cursor: pointer;

    .menu-title {
      display: flex;

      >img {
        margin-right: 20px;
      }
    }
  }

  .menu-item:hover {
    background-color: #e6f5f0;
    color: #333;
  }
}</style>